#include"Siruri.h"
Siruri::Siruri(void)
{
    dimens=1;
    p=new char[dimens];
    if(!p)
     {
         cout<<"Eroare la alocare!"<<endl;
         exit(1);
     }
     *p='\0';
}
Siruri::Siruri(char *sir)
{
    dimens=strlen(sir) +1;
    p=new char[dimens];
    if(!p)
    {
        cout<<"Eroare la alocare"<<endl;
        exit(1);
    }
    strcpy(p,sir);
}
Siruri::Siruri(const Siruri &obiect)
{
    dimens=obiect.dimens;
    p=new char[dimens];
    if(!p)
    {
        cout<<"Eroare la alocare"<<endl;
        exit(1);
    }
    strcpy(p,obiect.p);
}
ostream &operator<<(ostream &flux, Siruri &obiect)
{
    flux<<obiect.p;
    return flux;
}
Siruri Siruri::operator=(Siruri &obiect)
{
    Siruri temp(obiect.p);
    if(obiect.dimens>dimens)
    {
        delete p;
        p = new char[obiect.dimens];
        dimens=obiect.dimens;
        if(!p)
        {
            cout<<"Eroare la alocare"<<endl;
            exit(1);
        }
    }
    strcpy(p, obiect.p);
    strcpy(temp.p,obiect.p);
    return temp;
}

Siruri Siruri::operator=(char *s)
{
    int lungime=strlen(s) + 1;
    if(dimens<lungime)
    {
        delete p;
        p=new char[lungime];
        dimens=lungime;
        if(!p)
        {
            cout<<"Eroare la alocare"<<endl;
            exit(1);
        }
    }
    strcpy(p,s);
    return *this;
}

Siruri Siruri::operator+(Siruri &obiect)
{
    int lungime;
    Siruri temp;
    delete temp.p;
    lungime=strlen(obiect.p) + strlen(p) +1;
    temp.p=new char[lungime];
    temp.dimens = lungime;
    if(!temp.p)
    {
        cout<<"Eroare la alocare"<<endl;
        exit(1);
    }
    strcpy(temp.p, this->p);
    strcat(temp.p, obiect.p);
    return temp;
}
Siruri Siruri::operator+(char *s)
{
    int lungime;
    Siruri temp;

    delete temp.p;
    lungime=strlen(s)+strlen(p)+1;
    temp.p=new char[lungime];
    temp.dimens=lungime;
    if(!temp.p)
    {
        cout<<"Eroare la alocare"<<endl;
        exit(1);
    }
    strcpy(temp.p, this->p);
    strcat(temp.p,s);
    return temp;
}
Siruri operator+(char *s, Siruri &obiect)
{
    int lungime;
    Siruri temp;
    delete temp.p;
    lungime = strlen(s)+strlen(s) +1;
    temp.p=new char[lungime];
    temp.dimens=lungime;
    if(!temp.p)
    {
        cout<<"Eroare la alocare";
        exit(1);
    }
    strcpy(temp.p,s);
    strcat(temp.p, obiect.p);
    return temp;
}
Siruri Siruri::operator-(Siruri &subsir)
{
    Siruri temp(p);
    char *s1;
    int i,j;
    s1=p;
    for(i=0; *s1; i++)
    {
        if(*s1!=*subsir.p)
        {
            temp.p[i]=*s1;
            s1++;
        }
    else{
    for(j=0; subsir.p[j]==s1[j] && subsir.p[j]; j++);
    if(!subsir.p[j])
       {
           s1+=j;
           i--;
       }
       else
       {
           temp.p[i]=*s1;
           s1++;
       }

    }
    }
    temp.p[i]='\0';
    return temp;
}
Siruri Siruri::operator-(char *subsir)
{
    Siruri temp(p);
    char *s1;
    int i,j;
    for(i=0;*s1;i++)
    {
        if(*s1!=*subsir)
        {
            temp.p[i]=*s1;
            s1++;
        }
        else
        {
            for(j=0;subsir[j]==s1[j] && subsir[j]; j++);
            if (!subsir[j])
            {
                s1+=j;
                i--;
            }
            else
            {
                temp.p[i]=*s1;
                s1++;
            }
        }
    }
    temp.p[i]='\0';
    return temp;
}

